package com.sndscl.rpc.common.util;

import com.sndscl.rpc.common.dto.RpcRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

/**
 * @Description
 * @Author lsh
 * @Date 2020/8/11 16:14
 **/
public class ClazzInvockUtil {

    public static Set<String> getInterfaceMethodSign(Class<?> clazz){
        Set<String> interfaceSignSets = new HashSet<>();
        Method[] methods = clazz.getMethods();
        for(Method method :methods){
            String methodSign = getMethodSign(clazz,method);
            interfaceSignSets.add(methodSign);
        }
        return interfaceSignSets;
    }

    public static String getMethodSign(Class<?> clazz,Method method){
        String interfaceName = clazz.getName();
        String methodName = method.getName();
        StringBuffer stringBuffer = new StringBuffer();
        Class<?>[] parameterTypes = method.getParameterTypes();
        int paramNum = parameterTypes.length;
        int index = paramNum-1;
        for(int i = 0; i < paramNum; i++){
            Class<?> parameterClazz = parameterTypes[i];
            String parameterType = parameterClazz.getName();
            stringBuffer.append(parameterType);
            if(i != index){
                stringBuffer.append(",");
            }
        }
        return interfaceName+"_"+methodName+"_"+stringBuffer.toString();
    }
}
